<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/WEB-INF/jsp/common/taglibs.jsp" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <title>文件上传</title>
    <%@include file="/WEB-INF/jsp/common/meta2.jsp" %>

    <script>
        $(document).ready(function () {
            var fileObject = null;
            <c:if test="${fileObject != null}">
            fileObject = {
                id:${fileObject.id}, original: "${fileObject.original}", size:${fileObject.size}
            }
            if (top.setUploadFileData) {
                top.setUploadFileData(fileObject);
            }
            </c:if>

            $("#fileForm").submit(function () {
                if ($("input[name='files']").val()) {

                    if (parent != null && parent.ProgressBarUtil.isLoading) {
                        /* alert("其它文件上传中，请稍后再试！");  */
                        return false;
                    } else {
                        parent.ProgressBarUtil.isLoading = true;
                    }
                    parent.ProgressBarUtil.show("uploadPercent", 800, progressObject);
                } else {
                    alert("请选择要上传的文件！");
                    return false;
                }
            });


            top.window.UploadFileUtil = {
                alertTimes: 0,
                selectFile: function (reg, msg, progressObject) {
                    window.progressObject = progressObject;
                    var _self = this;
                    _self.alertTimes = 1;
                    $("input:file").click().one("change", function () {
                        var fileName = $(this).val();
                        if (reg.test(fileName)) {
                            _self.upload();
                        } else {
                            if (_self.alertTimes == 1) {
                                alert(msg);
                                _self.alertTimes = 0;
                            }
                        }
                    });
                },
                upload: function () {
                    if (this.checkFileSize($("input:file"))) {
                        $("#fileForm").submit();
                    }
                },
                checkFileSize: function ($fileInput) {
                    return true;
                },
                getFileSize: function (fileInput) {
//                    if (!$.support.leadingWhitespace) {
//                        var fso = new ActiveXObject("Scripting.FileSystemObject");
//                        return fso.GetFile($(fileInput).val()).size;
//                    } else {
                    return  fileInput.files[0].size;
//                    }
                }
            }
        });
    </script>
</head>
<body class="content-body">
<div class="container-fluid">

    <table class="table">
        <tr>
            <th colspan="20" class="bg-info text-center">
                文件上传
            </th>
        </tr>
        <tr>
            <td>

                <form id="fileForm" method="post" action="${ctx}/common/file/upload"
                      modelAttribute="uploadForm" enctype="multipart/form-data">


                    <p>请选择上传文件.</p>
                    <table class="table">
                        <tr>
                            <td><input name="files" type="file"/></td>
                        </tr>
                        <tr>
                            <td>名称：${fileObject.original}</td>
                        </tr>
                        <tr>
                            <td>
                                ${fileObject.json}
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input type="submit" class="btn btn-sm btn-info" value="上传"/>
                                <a href="${ctx}/common/file/demo">click demo</a>
                            </td>
                        </tr>
                    </table>
                </form>
            </td>
        </tr>
    </table>
</div>
</body>
</html>
